# apps/zhihu/strategies.py
from utils.base_strategies import DeleteStrategy, BaseDataQueryStrategy
from .models import ZhihuContent, ZhihuComment
from ..system.models import CrawlerStatus, CrawlerConfig
class ZhihuDeleteStrategy(DeleteStrategy):
    def delete_content(self, task_id):
        ZhihuContent.objects.filter(task_id=task_id).delete()

    def delete_comments(self, task_id):
        content_ids = ZhihuContent.objects.filter(task_id=task_id).values_list('content_id', flat=True)
        ZhihuComment.objects.filter(content_id__in=content_ids).delete()


class ZhihuDataQueryStrategy(BaseDataQueryStrategy):
    """知乎数据查询策略类"""
    
    def get_platform_data(self, task_id: str, **kwargs):
        """获取知乎平台数据"""
        try:
            # 获取爬虫配置信息
            crawler_config = CrawlerConfig.objects.get(task_id=task_id)
            print(task_id)
            # 根据爬虫配置获取知乎数据
            zhihu_data = ZhihuContent.objects.filter(
                task_id=task_id
            ).values(
                'question_id',
                'title',
                'desc',
                'content_text',
                'voteup_count',
                'comment_count',
                'created_time'
            )
            
            return {
                'code': 0,
                'data': list(zhihu_data),
                'message': 'success'
            }
            
        except CrawlerConfig.DoesNotExist:
            return {
                'code': 404,
                'message': '未找到对应的爬虫配置信息'
            }
        except Exception as e:
            return {
                'code': 500,
                'message': f'获取知乎数据失败: {str(e)}'
            } 